package com.rr.easy;

import com.rr.entity.TreeNode;

/**
 * @author zhuangcanrun
 * @Date 2022/05/30/21:37
 * @description
 **/


/*
 * https://leetcode.cn/problems/sum-of-root-to-leaf-binary-numbers/
 * */
public class SumRootToLeaf {

    int ans = 0;

    public int sumRootToLeaf(TreeNode root) {
        doGetSum(root, 0);
        return ans;
   }

    private void doGetSum(TreeNode root, int currentSum) {
        currentSum = currentSum * 2 + root.val;
        if (root.left == null && root.right == null) {
            ans += currentSum;
            return;
        }
        if (root.left != null) {
            doGetSum(root.left, currentSum);
        }
        if (root.right != null) {
            doGetSum(root.right, currentSum);
        }
    }
}
